package com.gogowan.agroenterprise.utils;

import android.app.Activity;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class GdLocationUtil {
    private AMapLocationClient mLocationClient;
    //定位参数设置，通过这个类可以对定位的相关参数进行设置在AMapLocationClient进行定位时需要这些参数
    private AMapLocationClientOption mLocationOption;
    private static GdLocationUtil util;
    private Activity activity;
    private MyLocationListener myLocationListener;
    public static GdLocationUtil init(Activity activity){
        util= new GdLocationUtil(activity);
        return util;
    }
    public GdLocationUtil(Activity activity){
        this.activity=activity;
        AMapLocationClient.updatePrivacyAgree(activity.getApplicationContext(), true);
        AMapLocationClient.updatePrivacyShow(activity.getApplicationContext(), true, true);
        try {
            mLocationClient = new AMapLocationClient(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setOnceLocation(true);
        //设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }
    public GdLocationUtil setOnResultListener(MyLocationListener listener){
        myLocationListener=listener;
        return this;
    }
    public GdLocationUtil start(){
        mLocationClient.setLocationListener(listener);
        mLocationClient.startLocation();
        return this;
    }
    private AMapLocationListener listener =new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            myLocationListener.onResult(aMapLocation);
            mLocationClient.stopLocation();
        }
    };
    public interface MyLocationListener{
        void onResult(AMapLocation aMapLocation);
    }
}
